% osl_brain_plot(fname,plot_type,slice_ind,vol,gridstep,bg_type, plot_name,plot_title,perc_thresh)
%
% function to produce summary plots for OSL.
%
% INPUTS:
% fname: the full path name of the .nii.gz volume.
% plot_type: either 'zslice_plot', 'xslice_plot', 'yslice_plot', 'all_views' or light box.
% slice_ind: the x,y or z slice index or 'max_vox' which uses picks the slice with the max abs value.
% vol: the time point or component index.
% gridstep: the image resolution
% bg_type: the type of background image ('brain' or 'mask');
% plot_name: the file name of the output plot.
% plot_title: the plot title
% perc_thresh: 2x1 vector of lower and upper percentile thresholds.

function osl_brain_plot(fname,plot_type,slice_ind,vol,gridstep,bg_type, plot_name,plot_title,perc_thresh)

global OSLDIR;
mapin=ra(fname);

switch bg_type
    case {'mask'}
        bgmap=ra([OSLDIR '/std_masks/MNI152_T1_' num2str(gridstep) 'mm_brain_mask']);
    case {'brain'}
        bgmap=ra([OSLDIR '/std_masks/MNI152_T1_' num2str(gridstep) 'mm_brain']);
    otherwise
        bgmap=ra([OSLDIR '/std_masks/MNI152_T1_' num2str(gridstep) 'mm_brain']);
end

percfrom=perc_thresh(1);percto=perc_thresh(2);


figure;
for v = 1:numel(vol);
    
map=mapin(:,:,:,vol(v));


switch plot_type

    case {'xslice_plot'}
        subplot(numel(vol),1,1+(v-1));
        if ischar(slice_ind) && strcmp(slice_ind,'max_vox');    [~,xslice_ind] = max(max(max(abs(map),[],3),[],2)); else xslice_ind = slice_ind; end
        x1=squash(map,abs(map));
        low=percentile((x1),percfrom);high=percentile((x1),percto);
        overlay_act(flipud(squeeze(map(xslice_ind,:,:))'), flipud(squeeze(bgmap(xslice_ind,:,:))'),'red2yellow',0,[low high],[3000 8000]);
        tit=plot_title;
        title([tit ', min=' num2str(low) ', max=' num2str(high)]);
    case {'yslice_plot'}
        subplot(numel(vol),1,1+(v-1));
        if ischar(slice_ind) && strcmp(slice_ind,'max_vox');    [~,yslice_ind] = max(max(max(abs(map),[],3),[],1)); else yslice_ind = slice_ind; end
        x1=squash(map,abs(map));
        low=percentile((x1),percfrom);high=percentile((x1),percto);
        overlay_act(flipud(squeeze(map(:,yslice_ind,:))'), flipud(squeeze(bgmap(:,yslice_ind,:))'),'red2yellow',0,[low high],[3000 8000]);
        tit=plot_title;
        title([tit ', min=' num2str(low) ', max=' num2str(high)]);
    case {'zslice_plot'}
        if ischar(slice_ind) && strcmp(slice_ind,'max_vox');    [~,zslice_ind] = max(max(max(abs(map),[],2),[],1)); else zslice_ind = slice_ind; end
        subplot(numel(vol),1,1+(v-1));
        x1=squash(map,abs(map));
        low=percentile((x1),percfrom);high=percentile((x1),percto);
        overlay_act(flipud(squeeze(map(:,:,zslice_ind))'), flipud(squeeze(bgmap(:,:,zslice_ind))'),'red2yellow',0,[low high],[3000 8000]);
        tit=plot_title;
        title([tit ', min=' num2str(low) ', max=' num2str(high)]);
    case {'all_views'}
        if ischar(slice_ind) && strcmp(slice_ind,'max_vox');    [~,xslice_ind] = max(max(max(abs(map),[],3),[],2)); else xslice_ind = slice_ind; end
        if ischar(slice_ind) && strcmp(slice_ind,'max_vox');    [~,yslice_ind] = max(max(max(abs(map),[],3),[],1)); else yslice_ind = slice_ind; end
        if ischar(slice_ind) && strcmp(slice_ind,'max_vox');    [~,zslice_ind] = max(max(max(abs(map),[],2),[],1)); else zslice_ind = slice_ind; end

        x1=squash(map,abs(map));
        low=percentile((x1),percfrom);high=percentile((x1),percto);
        
        subplot(numel(vol),3,1+(v-1)*3);
        overlay_act(flipud(squeeze(map(xslice_ind,:,:))'), flipud(squeeze(bgmap(xslice_ind,:,:))'),'red2yellow',0,[low high],[3000 8000]);
        tit=plot_title;
        title([tit ',x-view, min=' num2str(low) ', max=' num2str(high)]);
        
        subplot(numel(vol),3,2+(v-1)*3);
        overlay_act(flipud(squeeze(map(:,yslice_ind,:))'), flipud(squeeze(bgmap(:,yslice_ind,:))'),'red2yellow',0,[low high],[3000 8000]);
        tit=plot_title;
        title([tit ', y-view min=' num2str(low) ', max=' num2str(high)]);
        
        subplot(numel(vol),3,3+(v-1)*3);
        overlay_act(flipud(squeeze(map(:,:,zslice_ind))'), flipud(squeeze(bgmap(:,:,zslice_ind))'),'red2yellow',0,[low high],[3000 8000]);
        tit=plot_title;
        title([tit ',z-view, min=' num2str(low) ', max=' num2str(high)]);
        
        
    case {'light_box'}
        tit=plot_title;
        lightbox(map(:,:,20:2:70));colorbar;title(tit);
    otherwise 
        
end

end
print(gcf, '-dpng', [plot_name '_' plot_type]);


end